﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoClub.Core.Dao.Support;
using AutoClub.Core.Domain;
using AutoClub.Core.Domain.Dao;

namespace AutoClub.Core.Dao.Implementations
{
    /// <summary>
    /// Represents a Repository of Users.
    /// </summary>
    public class UsersRepo : BaseRepo<Guid, User>, IUsersRepo
    {
        #region Properties

        private IUserRolesRepo UserRolesRepo { get; set; }

        private ICarCategoriesRepo CarCategoriesRepo { get; set; }

        #endregion

        #region Constructor

        public UsersRepo(IUnitOfWork uof, IUserRolesRepo userRolesRepo, ICarCategoriesRepo carCategoriesRepo)
            : base(uof, (uof.Context as AutoClubDataContext).Users, new GuidIdentifierGenerator())
        {
            this.UserRolesRepo = userRolesRepo;
            this.CarCategoriesRepo = carCategoriesRepo;
        }

        #endregion

        #region Methods

        public IEnumerable<User> GetAllAdministrators()
        {
            var users = this.DataSet.Include("Rol").ToList();

            return users.Where(u => u.Rol.Value.Equals(UserRolEnum.Administrator))
                                .AsEnumerable();
        }

        public User Add(string email, string password, CarCategoryEnum category)
        {
            CarCategory categoryEntity = this.CarCategoriesRepo.Get(category);
            UserRol rol = this.UserRolesRepo.Get(UserRolEnum.Pilot);

            User result = new User(email, password, rol, categoryEntity);
            result.IsAdminApproved = false; //Debe ser aprobado por la administracion antes de poder ejecutar algo.

            return this.Add(result);
        }

        public bool IsUserValid(string email, string password)
        {
            User actual = this.GetBy(u => u.Email.Equals(email) && u.Password.Equals(password)).FirstOrDefault();

            return actual != default(User);
        }

        #endregion
    }
}
